<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	public function index($err = "")
	{
		if (!$this->check_login()) return;
		$data['page_title'] = '后台首页';
		$data['err_info'] = $err;
		$this->load->view('a_header', $data);		
		$this->load->view('a_index');
		$this->load->view('footer');	
	}
	
	public function check_login()
	{
		if ($this->session->userdata('user') != "") {
			return true;
		}
		else {
			$data['page_title'] = '请登录';
			$this->load->view('header', $data);	
			$this->load->view('sidebar');		
			$this->load->view('login');
			$this->load->view('footer');
			return false;
		}
	}
	
	public function login()
	{
		$user = $this->input->post('username');
		$pass = $this->input->post('password');
		$query = $this->db->get_where('webpage_users', array('UserName'=>$user, 'Password'=>$pass));
		$result = $query->result();
		
		if (count($result) > 0) {
			$this->session->set_userdata('user', $user);
			$this->index();
		}
		else {
			if (!$this->check_login()) return;
		}
	}
	
	public function lgout()
	{
		$this->session->set_userdata('user', "");
		if (!$this->check_login()) return;
	}
	
	public function llist_o($offset=0)
	{
		if (!$this->check_login()) return;
		$table = "webpage_office";
		
		$per_page = 10;
		$news = $this->Office->get_entries($per_page, $offset);
		
		$pagination['base_url'] = site_url('/admin/llist_o/');
		$pagination['total_rows'] = $this->Office->get_total_num();
		$pagination['per_page'] = $per_page; 
		$pagination['cur_page'] = $offset;
		
		$data['news'] = $news;
		$data['page_title'] = '中心教研室';
		$data['pagination'] = $pagination;
		$data['base'] = 'news';
		$data['real_table'] = $table;
		
		$this->load->view('a_header', $data);		
		$this->load->view('ca_list');
		$this->load->view('footer');
	}
	
	public function llist($type='news', $s_type="中心动态", $offset=0)
	{
		if (!$this->check_login()) return;
		$table = "table";
		$s_type = urldecode($s_type);
		if ($type == 'news' || $type == "graduate" || $type == "recruitnews") {
			$table = "webpage_".$type;
		}
		else if ($type == "academic" || $type == "course") {
			$table = "webpage_news";
		}
		else if ($type == "office") {
			$table = "webpage_office";
		}
		else {
			$this->index("干嘛呢，干嘛呢！？");
			return;
		}
		
		$per_page = 10;
		$news = $this->Vnews->get_entries($table, $per_page, $offset, $s_type);
		
		$pagination['base_url'] = site_url('/admin/llist/'.$type.'/'.$s_type.'/');
		$pagination['total_rows'] = $this->Vnews->get_total_num($table, $s_type);
		$pagination['per_page'] = $per_page; 
		$pagination['cur_page'] = $offset;
		
		$data['news'] = $news;
		$data['page_title'] = urldecode($s_type);
		$data['pagination'] = $pagination;
		$data['base'] = 'news';
		$data['table'] = $type;
		$data['real_table'] = $table;
		$data['type'] = $s_type;
		
		$this->load->view('a_header', $data);		
		$this->load->view('a_list');
		$this->load->view('footer');
	}
	
	public function edit_o($id = 1)
	{
		if (!$this->check_login()) return;
		$table = "webpage_office";
		
		$ee = $this->Office->get_entrie($id);
		$data['news'] = $ee[0];
		
		$data['page_title'] = '编辑-'.$data['news']->OrgName;		
		
		$data['action'] = '/admin/update_o';
		
		$this->load->view('a_header', $data);	
		$this->load->view('editor_o');
		$this->load->view('footer');
	}
	
	public function edit($id = 1, $type='news', $s_type="中心动态")
	{
		if (!$this->check_login()) return;
		$table = "table";
		$s_type = urldecode($s_type);
		if ($type == 'news' || $type == "graduate" || $type == "recruitnews") {
			$table = "webpage_".$type;
			if ($type == 'news') {
				$available_types = array('中心动态', '公告通知');
			} else if ($type == 'graduate') {
				$available_types = array('本科生教学', '研究生教学');
			} else if ($type == 'recruitnews') {
				$available_types = array('硕士招生', '硕士生复试');
			}
		}
		else if ($type == "academic" || $type == "course") {
			$table = "webpage_news";
			if ($type == 'academic') {
				$available_types = array('学术专题');
			} else {
				$available_types = array('精品课程');
			}
		}
		else if ($type == "office") {
			$table = "webpage_office";
		}
		else {
			$this->index("干嘛呢，干嘛呢！？");
			return;
		}
		
		$ee = $this->Vnews->get_entrie($table, $id);
		$data['news'] = $ee[0];
		
		$data['page_title'] = '编辑-'.$data['news']->Title;		
		
		$data['appendix'] = $this->Vnews->get_all_atts($table, $data['news']->NewsId, $data['news']->Type);
		$data['available_types'] = $available_types;
		$data['table'] = $type;
		$data['real_table'] = $table;
		$data['type'] = $s_type;
		$data['action'] = '/admin/update';
		
		$this->load->view('a_header', $data);	
		$this->load->view('editor');
		$this->load->view('footer');
	}
	
	public function addnew_o($type='news', $s_type="中心动态")
	{
		if (!$this->check_login()) return;
		$table = "webpage_office";
		
		$item = (array)new $this->Office;
		$item['OrgId'] = 0;
		$item = (object)$item;
		
		$item->OrgName = '请输入标题';
		$item->OrgIntro = '在此输入内容';
		$data['news'] = $item;
		
		$data['page_title'] = '添加-'.$data['news']->OrgName;		
		
		$data['action'] = '/admin/insert_o';
		
		$this->load->view('a_header', $data);	
		$this->load->view('editor_o');
		$this->load->view('footer');
	}
	
	public function addnew($type='news', $s_type="中心动态")
	{
		if (!$this->check_login()) return;
		$table = "table";
		$s_type = urldecode($s_type);
		if ($type == 'news' || $type == "graduate" || $type == "recruitnews") {
			$table = "webpage_".$type;
			if ($type == 'news') {
				$available_types = array('中心动态', '公告通知');
			} else if ($type == 'graduate') {
				$available_types = array('本科生教学', '研究生教学');
			} else if ($type == 'recruitnews') {
				$available_types = array('硕士招生', '硕士生复试');
			}
		}
		else if ($type == "academic" || $type == "course") {
			$table = "webpage_news";
			if ($type == 'academic') {
				$available_types = array('学术专题');
			} else {
				$available_types = array('精品课程');
			}
		}
		else if ($type == "office") {
			$table = "webpage_office";
		}
		else {
			$this->index("干嘛呢，干嘛呢！？");
			return;
		}
		
		$item = (array)new $this->Vnews;
		$item['NewsId'] = 0;
		$item = (object)$item;
		$item->Author = 'admin';
		$item->NewsDate = date('Y-m-d');
		$item->Title = '请输入标题';
		$item->Detail = '在此输入内容';
		$data['news'] = $item;
		
		$data['page_title'] = '添加-'.$data['news']->Title;		
		
		$data['appendix'] = array();
		$data['available_types'] = $available_types;
		$data['table'] = $type;
		$data['real_table'] = $table;
		$data['type'] = $s_type;
		$data['action'] = '/admin/insert';
		
		$this->load->view('a_header', $data);	
		$this->load->view('editor');
		$this->load->view('footer');
	}
	
	public function update_o()
	{
		if (!$this->check_login()) return;
		$url = $this->input->post('backurl');
		$this->Office->update_entry();
		redirect('/admin/llist_o/', 'refresh');
		//redirect($url, 'refresh');
	}
	
	public function update()
	{
		if (!$this->check_login()) return;
		$url = $this->input->post('backurl');
		if ($this->input->post('atts')) {
			$this->Vnews->add_atts();
		}
		if ($this->input->post('delete_atts')) {
			$this->Vnews->delete_atts();
		}
		$this->Vnews->update_entry();
		redirect('/admin/llist/'.$this->input->post('big_type').'/'.urlencode($this->input->post('type')), 'refresh');
		//redirect($url, 'refresh');
	}
	
	public function insert_o()
	{
		if (!$this->check_login()) return;
		
		if( $this->Office->insert_entry()) {
			$insert_id = $this->db->insert_id();
		} else {
			$this->index('好像出了点问题');
			return;
		}
		
		redirect('/admin/llist_o/', 'refresh');
	}
	
	public function insert()
	{
		if (!$this->check_login()) return;
		
		if( $this->Vnews->insert_entry()) {
			$insert_id = $this->db->insert_id();
		} else {
			$this->index('好像出了点问题');
			return;
		}
		
		if ($this->input->post('atts')) {
			$this->Vnews->add_atts($insert_id);
		}
		
		redirect('/admin/llist/'.$this->input->post('big_type').'/'.urlencode($this->input->post('type')), 'refresh');
	}
	
	public function delete_o()
	{
		if (!$this->check_login()) return;
		$url = $this->input->post('backurl');
		if ($this->input->post('delete')) {
			$this->Office->delete_entries();
		}
		redirect($url, 'refresh');
	}
	
	public function delete()
	{
		if (!$this->check_login()) return;
		$url = $this->input->post('backurl');
		if ($this->input->post('delete')) {
			$this->Vnews->delete_entries();
		}
		redirect($url, 'refresh');
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */